package com.resou.reader.mine.model;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.resou.reader.R;
import com.resou.reader.api.entry.SortSubBean;

import java.util.List;

/**
 * Created by ${Garrett} on 2018/10/2.
 * Contact me krouky@outlook.com
 */
public class FavoriteAdapter extends RecyclerView.Adapter<FavoriteAdapter.ViewHolder> {

    List<SortSubBean> list;

    public void setData(List<SortSubBean> data) {
        this.list = data;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_favorite_list, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        SortSubBean bean = list.get(i);
        if (bean == null) {
            return;
        }
        viewHolder.favoriteTv.setText(bean.getCategoryName());
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        TextView favoriteTv;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            favoriteTv = itemView.findViewById(R.id.favorite_text);
            favoriteTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    v.setSelected(!v.isSelected());
                }
            });
        }
    }
}
